package com.itboye.sunsun.forums.ui;

import java.util.ArrayList;
import java.util.List;

import com.itboye.sunsun.R;
import com.itboye.sunsun.Application.App;
import com.itboye.sunsun.R.drawable;
import com.itboye.sunsun.R.id;
import com.itboye.sunsun.R.layout;
import com.itboye.sunsun.adapter.ForumHoneGrideView;
import com.itboye.sunsun.adapter.ForumHoneGrideViewPet;
import com.itboye.sunsun.adapter.ViewPagerAdapter;
import com.itboye.sunsun.custome.ForumGridView;
import com.itboye.sunsun.forum.ui.ForumPostActivity;
import com.itboye.sunsun.forum.ui.ForumSearchActivty;
import com.itboye.sunsun.forum.ui.MyPostActivy;
import com.itboye.sunsun.person.ui.MyOrderActivity;
import com.itboye.sunsun.support.BaseFragment;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
/***
 * 论坛首页
 * @author 2016-03-02
 *
 */
public class ForumHoneFragment extends BaseFragment implements OnClickListener {
	private Context context;
	private ViewPagerAdapter mViewPagerAdapter;
	private ViewPager mViewPager;
	private boolean isLoop = true;
	private ViewGroup nViewGroup;

	private ImageView[] mImag_View;
	ForumGridView fGridViwe, gridView2;
	ArrayList<Bitmap> list;
	ArrayList<Bitmap> mlist;
	private ImageView mimg_search;// 论坛搜索
	private RelativeLayout mrelat_forum_shaitu;// 晒图
	private TextView mtext_forum_com;// 进入水族论坛
	private View allView;
	private RelativeLayout realt_forum;//招募版主

	public ForumHoneFragment() {
	}

	public ForumHoneFragment(Context context) {
		this.context = context;
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		allView = inflater.inflate(R.layout.frament_forum_hone_page, container,
				false);

		initView();
		return allView;
	}

	/*
	 * @Override protected void onCreate(Bundle savedInstanceState) { // TODO
	 * Auto-generated method stub super.onCreate(savedInstanceState);
	 * setContentView(R.layout.frament_forum_hone_page); initView(); }
	 */

	public void initView() {
		// 将5个布局文件填充为View
		LayoutInflater inflater = getActivity().getLayoutInflater();
		View view1 = inflater.inflate(R.layout.layout1, null);

		view1.setBackground(getResources().getDrawable(R.drawable.image1));
		View view2 = inflater.inflate(R.layout.layout2, null);
		View view3 = inflater.inflate(R.layout.layout3, null);
		View view4 = inflater.inflate(R.layout.layout4, null);
		View view5 = inflater.inflate(R.layout.layout5, null);

		// 将View添加到一个list集合中作为参数传递给Adapter
		List<View> mListViews = new ArrayList<View>();
		mListViews.add(view1);
		mListViews.add(view2);
		mListViews.add(view3);
		mListViews.add(view4);
		mListViews.add(view5);
		fGridViwe = (ForumGridView) allView.findViewById(R.id.gridview);
		gridView2 = (ForumGridView) allView.findViewById(R.id.gridview_pet);
		// 论坛搜索
		mimg_search = (ImageView) allView
				.findViewById(R.id.img_fourm_hone_news);
		mrelat_forum_shaitu = (RelativeLayout) allView
				.findViewById(R.id.relat_forum_shaitu);
		mtext_forum_com = (TextView) allView.findViewById(R.id.text_forum_com);
		realt_forum=(RelativeLayout) allView.findViewById(R.id.realt_forum);
		realt_forum.setOnClickListener(this);
		mtext_forum_com.setOnClickListener(this);
		mrelat_forum_shaitu.setOnClickListener(this);
		mimg_search.setOnClickListener(this);
		ForumHoneGrideViewPet mForumHoneGrideViewPet = new ForumHoneGrideViewPet(
				mlist, getActivity());
		ForumHoneGrideView adapter = new ForumHoneGrideView(list, getActivity());
		fGridViwe.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// TODO Auto-generated method stub
				switch (position) {
				case 0:
					Toast.makeText(getActivity(), "我是" + position, 0).show();
					break;
				case 1:
					Toast.makeText(getActivity(), "我是" + position, 0).show();
					break;
				case 2:
					Toast.makeText(getActivity(), "我是" + position, 0).show();
					break;
				case 3:
					Toast.makeText(getActivity(), "我是" + position, 0).show();
					break;
				case 4:
					Toast.makeText(getActivity(), "我是" + position, 0).show();
					break;
				case 5:
					Toast.makeText(getActivity(), "我是" + position, 0).show();
					break;

				default:
					break;
				}
			}
		});
		fGridViwe.setAdapter(adapter);
		gridView2.setAdapter(mForumHoneGrideViewPet);
		// 初始化控件ViewPager
		mViewPager = (ViewPager) allView.findViewById(R.id.viewpager);
		nViewGroup = (ViewGroup) allView.findViewById(R.id.viewgroup);
		// 初始化Adapter
		mViewPagerAdapter = new ViewPagerAdapter(mListViews, getActivity());
		mViewPager.setAdapter(mViewPagerAdapter);

		// 把几个点的图片放入到ViewGroup中去
		mImag_View = new ImageView[mListViews.size()];// 获取图片的数组长度
		// 循坏取出.
		for (int i = 0; i < mImag_View.length; i++) {
			ImageView imageView = new ImageView(getActivity());
			imageView.setLayoutParams(new LayoutParams(10, 10));
			mImag_View[i] = imageView;
			if (i == 0) {
				mImag_View[i].setBackgroundResource(R.drawable.lunbored);
			} else {
				mImag_View[i].setBackgroundResource(R.drawable.lunbowite);
			}

			LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
					new ViewGroup.LayoutParams(LayoutParams.WRAP_CONTENT,
							LayoutParams.WRAP_CONTENT));
			layoutParams.leftMargin = 5;
			layoutParams.rightMargin = 5;
			nViewGroup.addView(imageView, layoutParams);
		}

		/***
		 * 控制图片的自动切换
		 */
		// 控制图片自动切换
		class NewThread extends Thread {
			@Override
			public void run() {
				while (isLoop) {

					SystemClock.sleep(5000);
					handler.sendEmptyMessage(0);

				}
			}
		}
		final NewThread mNewThread = new NewThread();
		mNewThread.start();

		// viewpager添加事件监听器
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageScrollStateChanged(int arg0) {
				Log.d("haha", "--------changed:" + arg0);

			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				Log.d("haha", "-------scrolled arg0:" + arg0);
				Log.d("haha", "-------scrolled arg1:" + arg1);
				Log.d("haha", "-------scrolled arg2:" + arg2);
			}

			@Override
			public void onPageSelected(int cunrrentIndex) {
				Log.d("haha", "------selected:" + cunrrentIndex);

				for (int i = 0; i < mImag_View.length; i++) {
					if (i == (cunrrentIndex % 5)) {
						mImag_View[i]
								.setBackgroundResource(R.drawable.lunbored);
					} else {
						mImag_View[i]
								.setBackgroundResource(R.drawable.lunbowite);
					}
				}

			}
		});
		// 设置当前页卡

		mViewPager.setCurrentItem(mListViews.size() * 100);

	}

	// 控制自动切换图片
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			mViewPager.setCurrentItem(mViewPager.getCurrentItem() + 1);
		};
	};

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.img_fourm_hone_news:
			Intent mIntent = new Intent(getActivity(), ForumSearchActivty.class);
			startActivity(mIntent);
			break;
		case R.id.relat_forum_shaitu:
			Intent mIntentPost = new Intent(getActivity(),
					ForumPostActivity.class);
			startActivity(mIntentPost);
			Toast.makeText(getActivity(), "晒图评奖", Toast.LENGTH_SHORT).show();
			break;
		case R.id.text_forum_com:
			/**
			 * 点击进入水族论坛
			 * 
			 */
			Intent mIntente=new Intent();
			startActivity(mIntente);
			Toast.makeText(getActivity(), "进入帖子详情页", 0).show();

			break;
			
		case R.id.realt_forum:
			Intent intent0 = new Intent();
			intent0.setClass(App.ctx, MyPostActivy.class);
			intent0.putExtra("fragmentCurrent", 0);
			startActivity(intent0);
			break;
		default:
			break;
		}
	}
}

